package generator.src;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import generator.src.util.TaoUtil;

/**
 * 通过模板构建工程文件
 * @author 黄为涛
 *
 */
public class CodeBuilder implements Ibuilder{
	private static volatile CodeBuilder codeBuilder;
	
	private Configuration configration;
	
	private String userResourcePath;
	private String userCodePath;
	//generatorConfig.properties 中的变量
	private String domain ;
	private String sqlDriver;
	private String sqlUrl;
	private String sqlUser;
	private String sqlPassword;
	
	//初始化时加载配置文件
	private  CodeBuilder() {
		userResourcePath = System.getProperty("user.dir")+RESOURCES_PATH;
		userCodePath  =  System.getProperty("user.dir")+CODE_PATH;
		configration = new Configuration(Configuration.getVersion());
		try {
			//初始化模板配置对象
			configration.setDirectoryForTemplateLoading(new File(userResourcePath+"\\templates"));
			configration.setDefaultEncoding("utf-8");
			
			//获取generatorConfig.properties的值
			domain =TaoUtil.getProperty("domain");
			sqlDriver=TaoUtil.getProperty("sqlDriver");
			sqlUrl=TaoUtil.getProperty("sqlUrl");
			sqlUser=TaoUtil.getProperty("sqlUser");
			sqlPassword=TaoUtil.getProperty("sqlPassword");	
		} catch (IOException e) {
			
		}
	}	
	public static CodeBuilder getInstance() {
		if(codeBuilder==null) {
			synchronized(CodeBuilder.class) {
				if(codeBuilder==null) codeBuilder=new CodeBuilder();
			}
		}		
		return codeBuilder; //此时codeBuilder一定不为空
	}
	@Override
	public void creatSpringConfigFile() {
		try {
			String springConfigFile="applicationContext.xml";
			Template template = configration.getTemplate("Conf-applicationContextXML.ftl");
			
			Map<String,Object> map =new HashMap<>();
			map.put("config",this.codeBuilder);
			
			Writer out = new FileWriter(this.userResourcePath+springConfigFile);	
			//System.out.println(this.userResourcePath+springConfigFile);
			template.process(map, out);
			out.close();			
		} catch (IOException | TemplateException  e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void createSpringMVConfigFile() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void createDaoFiles() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void createMapperFiles() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void createServiceFiles() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void createDaoTestFiles() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void createServiceTestFiles() {
		// TODO Auto-generated method stub
		
	}
	public String getUserResourcePath() {
		return userResourcePath;
	}
	public String getUserCodePath() {
		return userCodePath;
	}
	public String getDomain() {
		return domain;
	}
	public String getSqlDriver() {
		return sqlDriver;
	}
	public String getSqlUrl() {
		return sqlUrl;
	}
	public String getSqlUser() {
		return sqlUser;
	}
	public String getSqlPassword() {
		return sqlPassword;
	}


}
